cc.Class({
    extends: cc.Component,

    properties: {
        tipLabel:cc.Label,
        dian:cc.Node,
        _progress:0.0,
    },

    // use this for initialization
    onLoad: function () {
        this.initMgr();
        this.tipLabel.string = "0%";
        this.checkVersion();
        
        for(var i = 0; i < this.dian.children.length;++i){
            this.dian.children[i].active = false;
        }
    },
    
    initMgr:function(){
        cc.vv = {};
        var UserMgr = require("UserMgr");
        cc.vv.userMgr = new UserMgr();
        
        var ReplayMgr = require("ReplayMgr");
        cc.vv.replayMgr = new ReplayMgr();
        
        cc.vv.http = require("HTTP");
        cc.vv.global = require("Global");
        cc.vv.net = require("Net");
        
        var GameNetMgr = require("GameNetMgr");
        cc.vv.gameNetMgr = new GameNetMgr();
        cc.vv.gameNetMgr.initHandlers();
        
        var AnysdkMgr = require("AnysdkMgr");
        cc.vv.anysdkMgr = new AnysdkMgr();
        cc.vv.anysdkMgr.init();
        
        var VoiceMgr = require("VoiceMgr");
        cc.vv.voiceMgr = new VoiceMgr();
        cc.vv.voiceMgr.init();
        
        var AudioMgr = require("AudioMgr");
        cc.vv.audioMgr = new AudioMgr();
        cc.vv.audioMgr.init();
        
        var Utils = require("Utils");
        cc.vv.utils = new Utils();
        
        cc.args = this.urlParse();
    },
    
    urlParse:function(){
        var params = {};
        if(window.location == null){
            return params;
        }
        var name,value; 
        var str=window.location.href; //取得整个地址栏
        var num=str.indexOf("?") 
        str=str.substr(num+1); //取得所有参数   stringvar.substr(start [, length ]
        
        var arr=str.split("&"); //各个参数放到数组里
        for(var i=0;i < arr.length;i++){ 
            num=arr[i].indexOf("="); 
            if(num>0){ 
                name=arr[i].substring(0,num);
                value=arr[i].substr(num+1);
                params[name]=value;
            } 
        }
        return params;
    },
    
    checkVersion:function(){
        var self = this;
        var onGetVersion = function(ret){
            if(ret.version == null){
                console.log("error.");
            }
            else{
                cc.vv.SI = ret;
                if(ret.version != cc.VERSION){
                    cc.find("Canvas/alert").active = true;
                }
                else{
                    self.startPreloading();
                }
            }
        };
        cc.vv.http.sendRequest("/get_serverinfo",null,onGetVersion);  
    },
    
    onBtnDownloadClicked:function(){
        cc.sys.openURL(cc.vv.SI.appweb);
    },
    
    startPreloading:function(){
        var self = this;
        cc.loader.loadResAll("textures", function (err, assets) {
            self.onLoadComplete();
        });
        cc.loader.onProgress = function ( completedCount, totalCount,  item ){
            //console.log("completedCount:" + completedCount + ",totalCount:" + totalCount );
            self._progress = completedCount/totalCount;
        };
        
        cc.loader.onComplete = function(error, items){
            self.onLoadComplete();
        }        
    },
    
    onLoadComplete:function(){
        cc.director.loadScene("login");
        cc.loader.onComplete = null;
    },

    // called every frame, uncomment this function to activate update callback
    update: function (dt) {
        for(var i = 0; i < this.dian.children.length;++i){
            this.dian.children[i].active = false;
        }
        var t = Math.floor(Date.now() / 1000) % this.dian.children.length;
        this.dian.children[t].active = true;
        //this.tipLabel.string = Math.floor(this._progress * 100) + "%";
    }
});